package com.example.administrator.mymusic;

import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.mymusic.Class.MyFragmentAdapter;
import com.example.administrator.mymusic.zhong.LrcFragment;
import com.example.administrator.mymusic.zhong.SongsFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2013/1/2.
 */
public class ZhongFragment extends Fragment implements ViewPager.OnPageChangeListener {
    private ViewPager viewPager;

    public ViewPager getViewPager() {
        return viewPager;
    }

    private List<Fragment> fragmentList;

    public List<Fragment> getFragmentList() {
        return fragmentList;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_zhong, null);
        viewPager = (ViewPager) view.findViewById(R.id.vp_zhong);
        fragmentList = new ArrayList<Fragment>();
        fragmentList.add(new SongsFragment());
        fragmentList.add(new LrcFragment());
        MyFragmentAdapter myFragmentAdapter = new MyFragmentAdapter(
                getActivity().getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(myFragmentAdapter);
//        viewPager.setPageTransformer(true,);
        viewPager.addOnPageChangeListener(this);
        LrcFragment lrcFragment = (LrcFragment) fragmentList.get(1);
        lrcBroadcastReceiver = lrcFragment.new LrcBroadcastReceiver();
        getActivity().registerReceiver(lrcBroadcastReceiver, new IntentFilter(ACTION_LRC));
        return view;
    }

    LrcFragment.LrcBroadcastReceiver lrcBroadcastReceiver;
    private static final String ACTION_LRC = "android.intent.action.LRC";

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        ShangFragment shangFragment = (ShangFragment) getActivity()
                .getSupportFragmentManager().findFragmentByTag("shangFragment");
        List<ImageView> imageViews = shangFragment.getImageViewList();
        TextView textViewBiaoTi = shangFragment.getTvBiaoTi();
        for (int i = 0; i < imageViews.size(); i++) {
            imageViews.get(i).setBackgroundColor(getResources().getColor(R.color.green));
        }
        imageViews.get(position).setBackgroundColor(getResources().getColor(R.color.black));
        switch (position) {
            case 0:
                textViewBiaoTi.setText("本地音乐");
                break;
            case 1:
                textViewBiaoTi.setText("歌词显示");
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(lrcBroadcastReceiver);
    }
}
